package twoD.IsoPfem.utils;

import inf.jlinalg.Vector2D;
import inf.jlinalg.Vector3D;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;

public class GiDMeshReader implements IMeshReader {

	private File file;

	public GiDMeshReader(String fileName) {
		file = new File(fileName);
	}

	@Override
	public ArrayList<Vector2D> getNodes2d() {
		ArrayList<Vector2D> nodes = new ArrayList<Vector2D>();
		Scanner scanner = null;
		try {
			scanner = new Scanner(new FileReader(file));
		} catch (FileNotFoundException e) {
			System.out.println("File not found");
			e.printStackTrace();
		}
		try {
			boolean startRead = false;
			// use a Scanner to get each line
			while (scanner.hasNextLine()) {
				String line = scanner.nextLine();
				if (line.compareTo("Coordinates") == 0) {
					startRead = true;
					continue;
				} else if (line.compareTo("End Coordinates") == 0)
					break;
				if (startRead) {
					Scanner lineScanner = new Scanner(line);
					if (lineScanner.hasNext()) {
						lineScanner.next(); // skip the index
						double x = Double.parseDouble(lineScanner.next());
						double y = Double.parseDouble(lineScanner.next());
						Vector2D v = new Vector2D(x, y);
						nodes.add(v);
					} else {
						System.out
								.println("Empty or invalid line. Unable to process.");
					}
				}
			}
		} finally {
			scanner.close();
		}
		return nodes;
	}

	@Override
	public ArrayList<Vector3D> getNodes3d() {
		ArrayList<Vector3D> nodes = new ArrayList<Vector3D>();
		Scanner scanner = null;
		try {
			scanner = new Scanner(new FileReader(file));
		} catch (FileNotFoundException e) {
			System.out.println("File not found");
			e.printStackTrace();
		}
		try {
			boolean startRead = false;
			// use a Scanner to get each line
			while (scanner.hasNextLine()) {
				String line = scanner.nextLine();
				if (line.compareTo("Coordinates") == 0) {
					startRead = true;
					continue;
				} else if (line.compareTo("End Coordinates") == 0)
					break;
				if (startRead) {
					Scanner lineScanner = new Scanner(line);
					if (lineScanner.hasNext()) {
						lineScanner.next(); // skip the index
						double x = Double.parseDouble(lineScanner.next());
						double y = Double.parseDouble(lineScanner.next());
						double z = Double.parseDouble(lineScanner.next());
						Vector3D v = new Vector3D(x, y, z);
						nodes.add(v);
					} else {
						System.out
								.println("Empty or invalid line. Unable to process.");
					}
				}
			}
		} finally {
			scanner.close();
		}
		return nodes;
	}

	@Override
	public ArrayList<ArrayList<Integer>> getElements() {
		ArrayList<ArrayList<Integer>> elements = new ArrayList<ArrayList<Integer>>();
		Scanner scanner = null;
		try {
			scanner = new Scanner(new FileReader(file));
		} catch (FileNotFoundException e1) {
			System.out.println("FIle not found");
			e1.printStackTrace();
		}
		try {
			boolean startRead = false;
			// use a Scanner to get each line
			while (scanner.hasNextLine()) {
				String line = scanner.nextLine();
				if (line.compareTo("Elements") == 0) {
					startRead = true;
					continue;
				} else if (line.compareTo("End Elements") == 0)
					break;
				if (startRead) {
					Scanner lineScanner = new Scanner(line);
					if (lineScanner.hasNext()) {
						lineScanner.next(); // skip the index
						ArrayList<Integer> e = new ArrayList<Integer>();
						while (lineScanner.hasNext())
							e.add(Integer.parseInt(lineScanner.next()));
						elements.add(e);
					} else {
						System.out
								.println("Empty or invalid line. Unable to process.");
					}
				}
			}
		} finally {
			scanner.close();
		}
		return elements;
	}

}
